package com.liruo.demo.serialize.jackson.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.liruo.demo.serialize.jackson.model.User;
import java.io.IOException;

/**
 * @Author:liruo
 * @Date:2023-02-20-02:14:43
 * @Desc
 */
public class StrUserSerializer extends JsonSerializer<User> {

  @Override
  public void serialize(User user, JsonGenerator jsonGenerator,
      SerializerProvider serializerProvider) throws IOException {
    if (user == null) {
      serializerProvider.defaultSerializeNull(jsonGenerator);
      return;
    }
    jsonGenerator.writeStartObject();
    jsonGenerator.writeStringField("name", "=大傻逼=" + user.getName());
    jsonGenerator.writeStringField("des", "=大傻逼=" + user.getName());
    jsonGenerator.writeEndObject();
  }

  @Override
  public Class<User> handledType() {
    return User.class;
  }
}
